package editManager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Scanner;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EditRun implements Serializable {
	
	private ArrayList<Edit> allEdits;
	private ArrayList<Edit> creditList;
	private ArrayList<Edit> orderChanged;
	private File editRunFile;
	
	public EditRun(File editFile) throws IOException{
		allEdits = new ArrayList<Edit>();
		creditList = new ArrayList<Edit>();
		orderChanged = new ArrayList<Edit>();
		this.editRunFile = editFile;
		
		Scanner scan = new Scanner(editFile);
		scan.useDelimiter(new String(new char[] {12}));
		
		while(scan.hasNext()){
			Edit edit = new Edit(scan.next());
			allEdits.add(edit);
			if(edit.isSentToCredit()){
				creditList.add(edit);
			}
			if(edit.isOrderChange()){
				orderChanged.add(edit);
			}
		}
		scan.close();
	}
	
	public void createFile(){

		DateFormat df = new SimpleDateFormat("MM-dd-yyyy HHmm");
		//String exportString = "V:\\kroach\\Edits Sent to Credit\\Sent to Credit " + df.format(new Date()) + ".rtf";
		//**************************************************** replace
		String exportString = "C:\\Users\\jedwards\\Desktop\\Development\\jtestFile.rtf";
		
		try {
			File exportS = new File(exportString);
			if(!exportS.exists()){ 
				exportS.createNewFile();
			}
			
			FileWriter fW = new FileWriter(exportS.getAbsoluteFile());
			BufferedWriter bW = new BufferedWriter(fW);
					
			for(Edit e : creditList){
				bW.write(e.getEditText());
				if (!e.equals(creditList.get((creditList.size() - 1)))){
					bW.write(new String(new char[] {12}));
				}
			}
			
			for(Edit e : orderChanged){
				bW.write(e.getEditText());
				if (!e.equals(orderChanged.get((orderChanged.size() - 1)))){
					bW.write(new String(new char[] {12}));
				}
			}
			bW.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void sendEmail(){
		String messageText ="";
		ArrayList<Edit> CCEditList = new ArrayList<Edit>();
		ArrayList<Edit> nonCCEditList = new ArrayList<Edit>();
		
		for(Edit e : creditList){
			if(e.isCreditCard()){
				CCEditList.add(e);
			} else{
				nonCCEditList.add(e);
			}
		}
		
		messageText = "Edits sent to Credit:" + "\n";
		
		for(Edit e : nonCCEditList){
			messageText = messageText + e.getCustomerNumber() + " " + e.getCustomerName() + " " + e.getCutNumber() + "\n";
		}
		
		if(CCEditList.size()>0){
			messageText = messageText + "Credit Cards:" + "\n";
			for(Edit e : CCEditList){
				messageText = messageText + e.getCustomerNumber()+ " " + e.getCustomerName() + " " + e.getCutNumber() + "\n";
			}
		}
		
		if(orderChanged.size()>0){
			messageText = messageText + "Order Changed:" + "\n";
			for(Edit e : orderChanged){
				messageText = messageText + e.getCustomerNumber()+ " " + e.getCustomerName() + " " + e.getCutNumber() + "\n";
			}
		}
		
		final String username = "edits@mariettadrapery.com";
		final String password = "3d!Ts22";
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		//props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "mail.mariettadrapery.com");
		props.put("mail.smtp.port", "25");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
		
		DateFormat df = new SimpleDateFormat("HH:mm");
		String formattedDate = df.format(new Date());
		
		try{
	         MimeMessage message = new MimeMessage(session);
	         message.setFrom(new InternetAddress("edits@mariettadrapery.com"));
	         message.addRecipient(Message.RecipientType.TO, new InternetAddress("ar@mariettadrapery.com"));
	         message.addRecipient(Message.RecipientType.CC, new InternetAddress("jedwards@mariettadrapery.com"));
	         message.setSubject("Sent to Credit " + formattedDate + " TEST");
	         BodyPart messageBodyPart = new MimeBodyPart();
	         messageBodyPart.setText(messageText);
	         Multipart multipart = new MimeMultipart();
	         MimeBodyPart attachpart = new MimeBodyPart();
	         String attachFile = "C:\\Users\\jedwards\\Desktop\\Development\\jtestFile.rtf";
	         attachpart.attachFile(attachFile);
	         multipart.addBodyPart(attachpart);
	         multipart.addBodyPart(messageBodyPart);
	         message.setContent(multipart);
	         //Transport.send(message);
	         System.out.println("Sent message successfully...");
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	      } catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}	

	public int size(){
		return allEdits.size();
	}

	public ArrayList<Edit> getAllEdits() {
		return allEdits;
	}

	public void setAllEdits(ArrayList<Edit> allEdits) {
		this.allEdits = allEdits;
	}

	public ArrayList<Edit> getCreditList() {
		return creditList;
	}

	public void setCreditList(ArrayList<Edit> creditList) {
		this.creditList = creditList;
	}

	public ArrayList<Edit> getOrderChanged() {
		return orderChanged;
	}

	public void setOrderChanged(ArrayList<Edit> orderChanged) {
		this.orderChanged = orderChanged;
	}


}
